'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');

class BrokerService extends Service {
    async getList() {
        const {
            ctx,
            app
        } = this;
        // const {list} = Mock.mock({
        //     "list|10":[
        //         {
        //             "name":"@cname",//姓名
        //             "tel":/[1][3-8]\d{9}/,//电话
        //             "state|1":[1,0]//状态
        //         }
        //     ]
        // });
        // list.forEach((item)=>{
        //     app.mysql.insert('brokers',{
        //         ...item
        //     })
        // })
        // return '123'
        const {
            pageSize,
            pageIndex,
            keyword,
            state
        } = ctx.query;
        let sql = `select * from brokers where 1=1 `;
        let sqlCount = `select count(id) as total from brokers where 1=1 `;
        // 状态
        if (state) {
            sql += `and state = '${state}' `
            sqlCount += `and state = '${state}' `
        }
        // 多字段查询
        if (keyword) {
            sql += `and concat(name,tel) like '%${keyword}%'`
            sqlCount += `and concat(name,tel) like '%${keyword}%'`
        }
        //分页
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        let res = await app.mysql.query(sql);
        let [{
            total
        }] = await app.mysql.query(sqlCount);
        // 创建一个promise数组
        let promiseArr = [];
        // 创建一个经纪人房源列表
        let brokerHouse = [];
        res.forEach((item, index) => {
            // 序号
            item.number = (pageIndex - 1) * 5 + index + 1;
            promiseArr.push(app.mysql.select('housing_resource', {
                where: {
                    broker: item.name
                }
            }))
        })
        await Promise.all(promiseArr).then((response) => {
            // response中每一项就是每一个项目经历所有的房源
            brokerHouse = response;
            res.forEach((item, index) => {
                item.newHouse = brokerHouse[index].filter((item1) => item1.house_type === '新房').length;
                item.rentHouse = brokerHouse[index].filter((item1) => item1.house_type === '出租房').length;
                item.secondHouse = brokerHouse[index].filter((item1) => item1.house_type === '二手房').length;
            })
        })

        return {
            data: res,
            total: total
        }
    }
    // 删除经纪人
    async delBroker () {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        let res = await app.mysql.delete('brokers', {
            id
        });
        return res
    }
    // 修改经纪人数据
    async brokerChange () {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            state,
        } = ctx.request.body;
        let row = {
            id
        };
        if (state !== undefined) {
            row.state = state
        }
        let res = await app.mysql.update('brokers', row);
        return res
    }
    // 新增经纪人
    async addBroker() {
        const {ctx,app} = this;
        const {
            tel,
            name,
            company,
            state,
        } = ctx.request.body;
        let res = await app.mysql.insert('brokers',{
            tel,
            name,
            company,
            state,
        });
        return res
    }
    // 编辑经纪人
    async editBroker () {
        const {ctx,app} = this;
        const {
            id,
            tel,
            name,
            company,
            state,
        } = ctx.request.body;
        let res = await app.mysql.update('brokers',{
            id,
            tel,
            name,
            company,
            state,
        });
        return res
    }
}

module.exports = BrokerService;